#include "node_mt.h"

#include <directedgraph.h>
#include <inodedata.h>
#include <node.h>

#include <QtTest/QTest>

using namespace core;
using namespace moduletest;

class DummyNodeData : public INodeData
{
public:
    DummyNodeData() : INodeData(), m_node(0) {}
    ~DummyNodeData() {}

    // INodeData
    virtual void registerNode(Node* node) { m_node = node; }
    const Node* const node() const { return m_node; }

public:
    Node* m_node;
};

void Node_mt::getData()
{
    DummyNodeData data;

    Node* n1 = Node::create(0);
    Node* n2 = Node::create(&data);

    QVERIFY(n2->getData() == &data && data.m_node == n2);

    delete n1;
    delete n2;
}

void Node_mt::getIncomingConnections()
{
    QFETCH(bool, value);
    QFETCH(bool, result);

    QCOMPARE(value, result);
}

void Node_mt::getIncomingConnections_data()
{
    DirectedGraph graph;

    Node* n1 = Node::create(0);
    Node* n2 = Node::create(0);
    Node* n3 = Node::create(0);
    Node* n4 = Node::create(0);

    graph.connect(n1, n3);
    graph.connect(n1, n2);
    graph.connect(n2, n3);

    const int first_size = 2;
    const int second_size = 3;
    const int third_size = 2;

    QTest::addColumn<bool>("value");
    QTest::addColumn<bool>("result");

    QSet<Node* > conns = n3->getIncomingConnections();

    QTest::newRow("2 connections") << ((conns.size() ==
        first_size) && conns.contains(n1) && conns.contains(n2)) << true;

    graph.connect(n4, n3);

    conns = n3->getIncomingConnections();

    QTest::newRow("3 connections") << ((conns.size() ==
        second_size) && conns.contains(n1) && conns.contains(n2) &&
        conns.contains(n4)) << true;

    graph.disconnect(n4, n3);

    conns = n3->getIncomingConnections();

    QTest::newRow("2 connections") << ((conns.size() ==
        third_size) && conns.contains(n1) && conns.contains(n2) &&
        !conns.contains(n4)) << true;
}

void Node_mt::getOutgoingConnections()
{
    QFETCH(bool, value);
    QFETCH(bool, result);

    QCOMPARE(value, result);
}

void Node_mt::getOutgoingConnections_data()
{
    DirectedGraph graph;

    Node* n1 = Node::create(0);
    Node* n2 = Node::create(0);
    Node* n3 = Node::create(0);
    Node* n4 = Node::create(0);

    graph.connect(n3, n1);
    graph.connect(n2, n1);
    graph.connect(n3, n2);

    const int first_size = 2;
    const int second_size = 3;
    const int third_size = 2;

    QTest::addColumn<bool>("value");
    QTest::addColumn<bool>("result");

    QSet<Node* > conns = n3->getOutgoingConnections();

    QTest::newRow("2 connections") << ((conns.size() ==
        first_size) && conns.contains(n1) && conns.contains(n2)) << true;

    graph.connect(n3, n4);

    conns = n3->getOutgoingConnections();

    QTest::newRow("3 connections") << ((conns.size() ==
        second_size) && conns.contains(n1) && conns.contains(n2) &&
        conns.contains(n4)) << true;

    graph.disconnect(n3, n4);

    conns = n3->getOutgoingConnections();

    QTest::newRow("2 connections") << ((conns.size() ==
        third_size) && conns.contains(n1) && conns.contains(n2) &&
        !conns.contains(n4)) << true;
}
